package com.yh.wuye.view;

import org.achartengine.GraphicalView;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout.LayoutParams;

public class PieChart extends BaseActivity
{
  private Button btnBack;
  private ViewGroup layoutChart;
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.pie_chart);
    
    btnBack = (Button)findViewById(R.id.btnBack);
    btnBack.setOnClickListener(onBack);
    
    layoutChart = (ViewGroup)findViewById(R.id.layoutChart);
    
    loadChart();
  }
  
  private OnClickListener onBack = new OnClickListener()
  {
    @Override
    public void onClick(View v)
    {
      finish();
    }
  };

  private void loadChart()
  {
    CategorySeries s1 = new CategorySeries("1");
    s1.add("水费", 12);
    s1.add("数字电视费",14);
    s1.add("电费",11);
    s1.add("煤气费",10);
    s1.add("宽带费",19);
    
    int[] colors = new int[] { Color.RED, Color.GREEN, Color.MAGENTA, Color.YELLOW, Color.CYAN };
    DefaultRenderer renderer = buildCategoryRenderer(colors);

    org.achartengine.chart.PieChart p = new org.achartengine.chart.PieChart(s1,renderer);
    
    GraphicalView gv = new GraphicalView(this, p);
    LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    gv.setLayoutParams(params);
    layoutChart.addView(gv);

  };
  
  private DefaultRenderer buildCategoryRenderer(int[] colors) {
    DefaultRenderer renderer = new DefaultRenderer();
    renderer.setLabelsTextSize(15);
    renderer.setLegendTextSize(15);
    renderer.setMargins(new int[] { 20, 30, 15, 0 });
    for (int color : colors) {
      SimpleSeriesRenderer r = new SimpleSeriesRenderer();
      r.setColor(color);
      renderer.addSeriesRenderer(r);
    }
    return renderer;
  }
}
